<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
       <el-table-column label="星期几" min-width="60">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.dayOfWeek`" :rules="formRules.dayOfWeek" class="mb-0px!">
            <el-select v-model="row.dayOfWeek" placeholder="请选择星期几">
                <el-option
                  v-for="dict in getStrDictOptions(DICT_TYPE.SCHOOL_DAY_OF_WEEK)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="快乐学习" align="center">
        <el-table-column label="集体教学活动" min-width="150">
          <template #default="{ row, $index }">
            <el-form-item :prop="`${$index}.happyLearningTeach`" :rules="formRules.happyLearningTeach" class="mb-0px!">
              <el-input v-model="row.happyLearningTeach" type="textarea" placeholder="请输入快乐学习-集体教学活动的内容" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="区域游戏" min-width="150">
          <template #default="{ row, $index }">
            <el-form-item :prop="`${$index}.happyLearningGame`" :rules="formRules.happyLearningGame" class="mb-0px!">
              <el-input v-model="row.happyLearningGame" type="textarea" placeholder="请输入快乐学习-区域游戏的内容" />
            </el-form-item>
          </template>
        </el-table-column>
      </el-table-column>
      
      <el-table-column label="快乐游戏" align="center">
        <el-table-column label="运动" min-width="150">
          <template #default="{ row, $index }">
            <el-form-item :prop="`${$index}.happyGameSport`" :rules="formRules.happyGameSport" class="mb-0px!">
              <el-input v-model="row.happyGameSport" type="textarea" placeholder="请输入快乐游戏-运动的内容" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="游戏" min-width="150">
          <template #default="{ row, $index }">
            <el-form-item :prop="`${$index}.happyGameGame`" :rules="formRules.happyGameGame" class="mb-0px!">
              <el-input v-model="row.happyGameGame" type="textarea" placeholder="请输入快乐游戏-游戏的内容" />
            </el-form-item>
          </template>
        </el-table-column>
      </el-table-column>
      
     
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加周计划每日内容</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { WeeklyPlanApi } from '@/api/school/weeklyplan'

const props = defineProps<{
  planId: undefined // 周计划ID（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
  planId: [{ required: true, message: '周计划ID不能为空', trigger: 'blur' }],
  dayOfWeek: [{ required: true, message: '星期几不能为空', trigger: 'change' }],
  sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.planId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await WeeklyPlanApi.getWeeklyPlanDailyListByPlanId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    planId: undefined,
    dayOfWeek: undefined,
    happyLearningTeach: undefined,
    happyLearningGame: undefined,
    happyGameSport: undefined,
    happyGameGame: undefined,
    sort: undefined,
    status: undefined,
  }
  row.planId = props.planId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>